﻿using System;
using System.Collections;
using System.Globalization;
using System.IO;

namespace TCL.FUNC
{
    public class Folder_File
    {
        public static String ReadFile(String sPath, String sRoot)
        {
            String sContent = "";
            try
            {
                if (System.Web.HttpContext.Current == null)
                {
                    sPath = sRoot + Convert.ToString(sPath).Replace("~/", "");
                }
                else
                {
                    sPath = System.Web.HttpContext.Current.Server.MapPath(sPath);
                }
                if (!String.IsNullOrEmpty(sPath))
                {
                    sContent = File.ReadAllText(sPath);
                    return sContent;
                }
            }
            catch
            {
            }
            return sContent;
        }

        public static Byte[] ReadAllBytes(String sPath)
        {
            Byte[] bRet =  Convert.FromBase64String("");
            try
            {
                if (!String.IsNullOrEmpty(sPath))
                    if (File.Exists(sPath))
                        bRet = File.ReadAllBytes(sPath);
            }
            catch
            {
            }
            return bRet;
        }

        public static Boolean WriteAllBytes(String sPath, Byte[] bContent, Boolean bOverride = true)
        {
            Boolean bRet = false;
            try
            {
                if (!String.IsNullOrEmpty(sPath))
                    if (File.Exists(sPath))
                    {
                        if (bOverride)
                            File.Delete(sPath);
                        else
                            return false;
                    }
                File.WriteAllBytes(sPath, bContent);
            }
            catch
            {
            }
            return bRet;
        }

        public static Boolean WriteFile(String sPath, String sContent)
        {
            try
            {
                sPath = System.Web.HttpContext.Current.Server.MapPath(sPath);
                if (!String.IsNullOrEmpty(sPath))
                {
                    File.WriteAllText(sPath, sContent, System.Text.Encoding.UTF8);
                    return true;
                }
            }
            catch
            {
            }
            return false;
        }

        public static String getFullFolder(String sFolder, Int16 iType)
        {
            if (!sFolder.EndsWith("\\"))
                sFolder += "\\";
            sFolder += iType.ToString().Substring(0, 1) + "_";
            return sFolder;
        }

        public static String getFileNameByDateTime(Int16 iFrequent = ConstEx.Log_iType_NameFile_DateTime_C.Day, String sExtend = ".nmt")
        {
            String sRet = "";
            switch (iFrequent)
            {
                case ConstEx.Log_iType_NameFile_DateTime_C.Default:
                    sRet = "temp";
                    break;
                case ConstEx.Log_iType_NameFile_DateTime_C.Year:
                    sRet = DateTime.Now.ToString("yyyy");
                    break;
                case ConstEx.Log_iType_NameFile_DateTime_C.Quarter:
                    sRet = DateTime.Now.ToString("yyyy") + ((DateTime.Now.Month - 1) / 3 + 1).ToString();
                    break;
                case ConstEx.Log_iType_NameFile_DateTime_C.Month:
                    sRet = DateTime.Now.ToString("yyyyMM");
                    break;
                case ConstEx.Log_iType_NameFile_DateTime_C.Week:
                    CultureInfo ciCurr = CultureInfo.CurrentCulture;
                    Int32 weekNum = ciCurr.Calendar.GetWeekOfYear(DateTime.Now, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
                    sRet += weekNum.ToString();
                    break;
                case ConstEx.Log_iType_NameFile_DateTime_C.Day:
                    sRet = DateTime.Now.ToString("yyyyMMdd");
                    break;
                case ConstEx.Log_iType_NameFile_DateTime_C.Hour:
                    sRet = DateTime.Now.ToString("yyyyMMddHH");
                    break;
                case ConstEx.Log_iType_NameFile_DateTime_C.Minute:
                    sRet = DateTime.Now.ToString("yyyyMMddHHmm");
                    break;
                case ConstEx.Log_iType_NameFile_DateTime_C.Second:
                    sRet = DateTime.Now.ToString("yyyyMMddHHmmss");
                    break;
                default:
                    break;
            }
            sRet += sExtend;
            return sRet;
        }

        public static SortedList getListFileInFolder(String strPath, String strExtend = "*.nmt")
        {
            SortedList slRet = new SortedList();
            try
            {
                if (strExtend.StartsWith("."))
                    strExtend = "*" + strExtend;
                if (!strExtend.StartsWith("*."))
                    strExtend = "*." + strExtend;

                if (Directory.Exists(strPath))
                {
                    string[] lstFile;
                    if (String.IsNullOrEmpty(strExtend))
                        lstFile = Directory.GetFiles(strPath);
                    else
                        lstFile = Directory.GetFiles(strPath, strExtend);
                    foreach (string item in lstFile)
                    {
                        slRet.Add(item, Path.GetFileNameWithoutExtension(item));
                    }
                }
            }
            catch
            {
            }
            return slRet;
        }
    }
}
